<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/6/14
 * Time: 10:55
 */

namespace Weixin\Model\Weixin;


use Think\Model;

class WeixinModel extends Model
{
    protected $tablePrefix = '';

    public function isWeixinExist($openid)
    {
        $map['openid'] = $openid;
        return $this->field('id')->where($map)->find();
    }

    public function findWeixin($openid)
    {
        $map['openid'] = $openid;
        return $this->field(true)->where($map)->find();
    }

    public function addWeixin($data)
    {
        return $this->data($data)->add();
    }

    public function updateWeixin($openid, $nickname, $update_time, $headimgurl, $subscribe)
    {
        $map['openid'] = $openid;
        $data['nickname'] = $nickname;
        $data['update_time'] = $update_time;
        $data['headimgurl'] = $headimgurl;
        $data['subscribe'] = $subscribe;
        return $this->where($map)->save($data);
    }

    /*public function updateSubscribe($openid, $subscribe)
    {
        $map['openid'] = $openid;
        $data['subscribe'] = $subscribe;
        return $this->where($map)->save($data);
    }*/

    public function subscribe($openid)
    {
        $map['openid'] = $openid;
        $data['subscribe'] = true;
        return $this->where($map)->save($data);
    }

    public function unsubscribe($openid)
    {
        $map['openid'] = $openid;
        $data['subscribe'] = false;
        return $this->where($map)->save($data);
    }

    public function getWeixinId($openid)
    {
        $map['openid'] = $openid;
        return $this->where($map)->field('id')->find();
    }
}